<?php

class ScheduleController extends ControllerModuleBackup
{
	public function actionIndex()
	{
		$backupScheduleAddForm = new BackupScheduleAddForm();
		$backupSchedule = new BackupSchedule();
		$now = time();
		
		$postData = app()->request->getPost('BackupScheduleAddForm', null);
		if($postData){
			$backupScheduleAddForm->attributes = $postData;
			$ret = false;
			$detail = $errors = null;
			if($backupScheduleAddForm->validate()){
				$backupSchedule->name = $postData['name'];
				$backupSchedule->id_backup_locations = $postData['id_backup_locations'];
				$backupSchedule->email = $postData['email'];
				$backupSchedule->delay = $postData['delay'];
				$backupSchedule->status = $postData['status'];
				$backupSchedule->hour = $postData['hour'];
				$backupSchedule->cpanel_accounts = implode(',', $postData['cpanel_accounts']);
				$backupSchedule->initDays($postData['days']);
				
				$ret = $backupSchedule->save();
				$detail = $backupSchedule->attributes;
				$detail['locationName'] = empty($backupSchedule->BackupLocation)? "" : $backupSchedule->BackupLocation->name;
				$detail['statusTxt'] = $backupSchedule->statusTxt;
				$detail['daysTxt'] = $backupSchedule->daysTxt;
				if(!$ret){
					$errors = $backupSchedule->getErrors();
				}
			}else{
				$errors = $backupScheduleAddForm->getErrors();
			}
			
			$this->_returnAjax(
				array(
					'result' => $ret,
					'detail' => $detail,
					'errors' => $errors
				)
			);
		}
		
		$this->render('index', array(
			'backupScheduleAddForm' => $backupScheduleAddForm,
			'backupScheduleEditForm' => new BackupScheduleEditForm(),
			'sort' => app()->request->getParam('ackupSchedule_sort', 'name_sort'),
			'backupSchedule' => $backupSchedule
		));
	}
	
	public function actionEdit()
    {
		if(app()->request->isAjaxRequest){
	        $postData = app()->request->getParam('BackupScheduleEditForm', null);
	    	if($postData){
	    		$backupScheduleEditForm = new BackupScheduleEditForm();
	    		$backupScheduleEditForm->attributes = $postData;
				$ret = false;
				$detail = $errors = null;
				$backupSchedule = $this->loadModelByPk($postData['id_backup_schedule'], 'BackupSchedule');

		    	if($backupScheduleEditForm->validate()){
			    	$backupSchedule->name = $postData['name'];
					$backupSchedule->id_backup_schedule = $postData['id_backup_schedule'];
					$backupSchedule->email = $postData['email'];
					$backupSchedule->delay = $postData['delay'];
					$backupSchedule->status = $postData['status'];
					$backupSchedule->hour = $postData['hour'];
					$backupSchedule->cpanel_accounts = implode(',', $postData['cpanel_accounts']);
					$backupSchedule->initDays($postData['days']);
					
					$ret = $backupSchedule->save();
					$detail = $backupSchedule->attributes;
					$detail['locationName'] = empty($backupSchedule->BackupLocation)? "" : $backupSchedule->BackupLocation->name;
					$detail['statusTxt'] = $backupSchedule->statusTxt;
					$detail['daysTxt'] = $backupSchedule->daysTxt;
					if(!$ret){
						$errors = $backupSchedule->getErrors();
					}
				}else{
					$errors = $backupScheduleEditForm->getErrors();
				}
				
            	$this->_returnAjax(array(
            		'result' => $ret,
					'detail' => $detail,
					'errors' => $errors
				));
			}
		}
    }
	
	public function actionDetail($id_backup_schedule)
	{
		$id_backup_schedule = app()->request->getParam('id_backup_schedule', null);
		if($id_backup_schedule){
			if(app()->request->isAjaxRequest){
				$backupSchedule = $this->loadModelByPk($id_backup_schedule, 'BackupSchedule');
				$this->_returnAjax(
					$backupSchedule->attributes
				);
			}
		}
	}
	
	public function actionDelete()
	{
		if(app()->request->isAjaxRequest){
			$id_backup_schedule = app()->request->getPost('id_backup_schedule', null);
			if($id_backup_schedule){
				$backupSchedule = $this->loadModelByPk($id_backup_schedule, 'BackupSchedule');
				
				$this->_returnAjax(array(
					'result' => $backupSchedule->delete(),
					'errors' => $backupSchedule->getErrors()
				));
			}
		
			$this->_end();
		}
	}
	
	public function accessRules()
	{
		return array(
			array(
				'allow',
				'actions' => array('index', 'delete', 'detail', 'edit'),
				'expression' => 'app()->user->isLogin() && app()->user->hasManageBackupPerm()',
				'redirect' => array('default/noaccess')
			),

			array(
				'deny',	//should be 'deny', 'allow' is used to give convenient to make test
				'users' => array('*'),
				'redirect' => array('user/login')
			)
		);
	}
}